package com.wy.controller.wy;

import com.wy.model.sys.ComplainEntity;
import com.wy.model.sys.ComplainLog;
import com.wy.model.sys.PageResult;
import com.wy.model.sys.UserEntity;
import com.wy.service.sys.UserService;
import com.wy.service.wy.ComplainService;
import com.wy.utils.BaseResponseInfo;
import com.wy.utils.FileUtil;
import com.wy.utils.HTTPStatus;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/complains")
public class ComplainController {
    @Resource(name = "complainServiceImpl")
    private ComplainService complainService;

    @Resource(name = "userServiceImpl")
    private UserService userService;

    /**
     * 投诉列表
     * @param pageSize
     * @param page
     * @param content
     * @param type
     * @param status
     * @param operator
     * @param complainTimeStart
     * @param complainTimeEnd
     * @return
     */
    @GetMapping("")
    public BaseResponseInfo complainList(HttpServletRequest request, @RequestParam(value = "pageSize",required = false) Integer pageSize,
                                         @RequestParam(value = "page",required = false) Integer page,
                                         @RequestParam(value = "content",required = false) String content,
                                         @RequestParam( value = "type",required = false)String type,
                                         @RequestParam(value = "status",required = false)String status,
                                         @RequestParam(value = "operator",required = false)String operator,
                                         @RequestParam(value = "complainTimeStart",required = false)String complainTimeStart,
                                         @RequestParam(value = "complainTimeEnd",required = false)String complainTimeEnd,
                                         @RequestParam(value = "uid",required = false)Integer uid){
        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
        try {
            List<String> rids = new ArrayList<>();
            UserEntity userEntity = userService.getUserByLoginName(request.getUserPrincipal().getName());
            if(null != userEntity) {
                String rid = userEntity.getRid();
                if(!StringUtils.isEmpty(rid)){
                    List list = Arrays.asList(rid.split(","));
                    rids = new ArrayList(list);
                }
            }
            PageResult pageResult = new PageResult();
            pageResult.setData(complainService.complainList(pageSize,pageSize*page,content,type,status,operator,complainTimeStart,complainTimeEnd,uid,rids));
            pageResult.setTotalCount(complainService.complainSize(pageSize,pageSize*page,content,type,status,operator,complainTimeStart,complainTimeEnd,uid,rids));
            baseResponseInfo.data = pageResult;
        }catch(Exception ex) {
            baseResponseInfo.code = HTTPStatus.INTERNAL_SERVER_ERROR;
            baseResponseInfo.data = ex.getMessage();
            ex.printStackTrace();
        }
        return baseResponseInfo;
    }

    /**
     *新增投诉
     * @param complainEntity
     * @return
     */
    @PostMapping("/complain")
    public ComplainEntity insertComplain(@RequestBody ComplainEntity complainEntity){
        complainEntity.setStatus("0");
        complainEntity.setImg(FileUtil.getFileName(complainEntity.getImg()));
        complainEntity.setStartTime(new Date());
        complainService.insertComplain(complainEntity);
        return complainEntity;
    }
    /**
     * 修改投诉
     * @param complainEntity
     * @param id
     * @return
     */
    @PutMapping("{id}")
    public ComplainEntity updateComplain(@RequestBody ComplainEntity complainEntity ,@PathVariable int id){
        if(complainEntity.getId() == id){
            complainEntity.setImg(FileUtil.getFileName(complainEntity.getImg()));
            complainService.updateComplain(complainEntity);
        }
        return  complainEntity;
    }

    @DeleteMapping("/del")
    public void  delRepair(@RequestBody List<String> groupId){
        complainService.delComplain(groupId);
    }

    @PostMapping("/complainLog")
    public ComplainLog insertComplain(@RequestBody ComplainLog complainLog){
        complainService.insertComplainLog(complainLog);
        return complainLog;
    }


}
